package com.naja.auth2clientfirst.config;

import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.naja.auth2clientfirst.support.IntegerMixin;
import com.naja.auth2clientfirst.support.ByteMixin;
import com.naja.auth2clientfirst.support.LongMixin;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.security.jackson2.SecurityJackson2Modules;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;

import java.util.List;

/**
 * @auther wangjianying
 * @date 2023/12/19 14:50
 */
@Configuration
//@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 600)
public class RedisHttpSessionConfig {
    @Bean
    public CookieSerializer cookieSerializer() {
        DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
//        cookieSerializer.setDomainName("");
        cookieSerializer.setCookieName("ZTSESSION");
        return cookieSerializer;
    }

    /**
     * bean名称必须为:springSessionDefaultRedisSerializer
     *
     * @return
     */
    @Bean("springSessionDefaultRedisSerializer")
    public RedisSerializer<Object> springSessionDefaultRedisSerializer(ObjectMapper oauth2ObjectMapper) {
        return new GenericJackson2JsonRedisSerializer(oauth2ObjectMapper);
    }
    @Bean("oauth2ObjectMapper")
    public ObjectMapper oauth2ObjectMapper(Jackson2ObjectMapperBuilder builder) {
        List<Module> securityModules = SecurityJackson2Modules.getModules(this.getClass().getClassLoader());
        builder.modules(securityModules);
        builder.mixIn(Long.class, LongMixin.class);
        builder.mixIn(Byte.class, ByteMixin.class);
        builder.mixIn(Integer.class, IntegerMixin.class);
        return builder.build();
    }

}
